package com.ledtechdesign.beans;

import java.util.Map;

public class Account {

	public static final int ALL = 0;
	public static final int ROOMS = 1;
	public static final int TEMPLATES = 2;

	private String accid;
	private String accountName;
	// in meeting Info: <accountPath
	// href="/accounts/bkf/na2-sdk-e98b1aa0-7375-421a-986a-a5f4f0cd75f4"/>
	private String accountPath;

	private boolean ready = false;

	// <room instance="na2-sdk-e98b1aa0-7375-421a-986a-a5f4f0cd75f4/#room#"/>
	private String roomInstanceString;

	private Map<String, Item> rooms;
	private Map<String, Item> templates;
	
	private String secret;
	private String url;

	public Account(String url, String account) {
		super();
		this.url = url;
		this.accountName = account;
	}

	public void flushItems(int cond) {
		switch (cond) {
		case ROOMS:
			this.rooms.clear();
			break;
		case TEMPLATES:
			this.templates.clear();
			break;
		default:
			this.rooms.clear();
			this.templates.clear();
			break;
		}
	}

	public String getAccid() {
		if (this.isReady()) {
			return this.accid;
		} else {
			throw new RuntimeException("Account is not ready yet,accountPath"
					+ "  and/or"
					+ "roomInstanceString are missing call this method "
					+ "when they are ready");
		}
	}

	public String getAccountName() {
		// if(isReady()){
		return this.accountName;
		// }else {
		// throw new RuntimeException("Account is not ready yet,accountPath" +
		// "  and/or" +
		// "roomInstanceString are missing call this method " +
		// "when they are ready");
		// }

	}

	public String getAccountPath() {
		return this.accountPath;
	}

	public String getRoomInstanceString() {
		return this.roomInstanceString;
	}

	public Map<String, Item> getRooms() {
		if (this.isReady()) {
			return this.rooms;
		} else {
			throw new RuntimeException("Account is not ready yet,accountPath"
					+ "  and/or"
					+ "roomInstanceString are missing call this method "
					+ "when they are ready");
		}
	}

	// TODO Secure it with SYSAUTHToken
	public String getSecret() {
		if (this.isReady()) {
			return this.secret;
		} else {
			throw new RuntimeException("Account is not ready yet,accountPath"
					+ "  and/or"
					+ "roomInstanceString are missing call this method "
					+ "when they are ready");
		}
	}

	public Map<String, Item> getTemplates() {
		if (this.isReady()) {
			return this.templates;
		} else {
			throw new RuntimeException("Account is not ready yet,accountPath"
					+ "  and/or"
					+ "roomInstanceString are missing call this method "
					+ "when they are ready");
		}
	}

	public String getUrl() {
		// if(isReady()){
		return this.url;
		// }else {
		// throw new RuntimeException("Account is not ready yet,accountPath" +
		// "  and/or" +
		// "roomInstanceString are missing call this method " +
		// "when they are ready");
		// }
	}

	public String getUrlAndAccount() {
		return this.url + "/" + this.accountName;
	}

	public boolean hasRoom() {
		return this.roomInstanceString != null;
	}

	public void init(String accid, String accountPath, String roomIsntParam) {

		if (accid == null || accountPath == null) {
			throw new RuntimeException(
					"Initializing Account with null paramener(s) "
							+ "is not possible, this is Seriouse issue"
							+ "accid: " + accid + " or accountPath "
							+ accountPath);
		}

		this.accid = accid;
		this.accountPath = accountPath;
		this.roomInstanceString = roomIsntParam;
		this.ready = true;
	}

	public boolean isReady() {
		return this.ready;
	}

	public void setAccid(String accid) {
		if (accid == null && this.accid != null) {
			throw new RuntimeException("you cant null accid, common now");
		}
		this.accid = accid;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public void setAccountPath(String accountPath) {
		this.accountPath = accountPath;
	}

	public void setRoomInstanceString(String roomInstanceString) {
		this.roomInstanceString = roomInstanceString;
	}

	public void setRooms(Map<String, Item> rooms) {
		this.rooms = rooms;
	}

	// TODO Secure it with SYSAUTHToken so that only accountManager cat get it
	public void setSecret(String secret) {
		this.secret = secret;
	}

	public void setTemplates(Map<String, Item> templates) {
		this.templates = templates;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return this.getUrlAndAccount();
	}

	public void update(String url, String accountName) {

	}

}
